var app = getApp()
import ajaxhelper from '../../utils/ajaxhelper.js';
var Quantity = require('../common/quantity/index.js');
Page(Object.assign({}, Quantity, {

  /**
   * 页面的初始数据
   */
  data: {
    quantity: {
      quantity: 1,
      min: 1,
      max: 20
    },
    count: 1,
    isShowGiftMask:false,//是否显示赠送确认框
    isDisabledButton:false,//赠送按钮是否禁用
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sourceId = options.sourceId;
    var coverMain = options.coverMain;
    var buyCount = options.count;
    var name = options.name;
    var orderId = options.orderId||'';//从购买赠送进入会有订单id，从个人的未赠送礼包进来直接会有礼包id
    var quantity = this.data.quantity;
    quantity.max = buyCount;
    var pkgId = options.pkgId||'';//礼包id，不一定会有
    this.setData({
      sourceId,
      coverMain,
      buyCount,
      quantity,
      orderId,
      pkgId,
      name
    })
    if (!pkgId){
      this.getPkgId();
    }
  },
  handleZanQuantityChange(e) {
    // 加数量之前根据后台返回的确认下
    var that=this;
    var componentId = e.componentId;
    var quantity = e.quantity;
    that.setData({
      [`${componentId}.quantity`]: quantity,
      count: quantity
    });
    // that.goGiving(quantity,function(){
    //   that.setData({
    //     [`${componentId}.quantity`]: quantity,
    //     count: quantity
    //   });
    // });
    
      
  },
  // 分享
  onShareAppMessage: function (shareRes) {
    var that=this;
    var count = that.data.count;
    var coverMain = that.data.coverMain;
    var pkgCutId = that.data.pkgCutId;
    var name=that.data.name;
    var getUserInfo = wx.getStorageSync('getUserInfo');
    if (getUserInfo) {
      var userName = getUserInfo.nickName;
      var avatarUrl = getUserInfo.avatarUrl;
    } 
    var date=+new Date();
    if (shareRes.from === 'button') {
      // 来自页面内转发按钮
      console.log(shareRes.target);
      console.log('/pages/myGiftCard/myGiftCard?pkgCutId=' + pkgCutId + '&userName=' + userName + '&avatarUrl=' + avatarUrl + '&date=' + date + '&coverMain=' + coverMain + '&name=' + name)
      return {
        title: '结果大学',
        path: '/pages/myGiftCard/myGiftCard?pkgCutId=' + pkgCutId + '&userName=' + userName + '&avatarUrl=' + avatarUrl + '&date=' + date + '&coverMain=' + coverMain + '&name=' + name,
        // imageUrl: cover,//测试 使用截图
        success: function (res) {
          // 转发成功
          console.log('zengs'+res);
          // 赠送成功页面
          wx.redirectTo({
            url: '../giftSuccess/giftSuccess?count=' + count + '&coverMain=' + coverMain,
          })
          console.log('zengs2' + res);
        },
        fail: function (res) {
          // 转发失败
          console.log(res)
        }
      }
    }
    
  },
  // 根据count赠送 先调接口通知后台要送多少分然后在弹框分享
  goGiving: function () {
    var that = this;
    var pkgId = that.data.pkgId;
    var count = that.data.count;
    var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    ajaxhelper.put(app.globalData.frontJSHost + 'present/send/' + pkgId + '/' + count + '?token=' + encodeURIComponent(token), {}, this, function (res) {
      console.log(res);
      if (res.result && res.resultStatus == 200) {
        if (res.data){
          that.setData({
            pkgCutId: res.data,
            isShowGiftMask:true,
          })
        }
        // if (successFun && Object.prototype.toString.call(successFun) === '[object Function]')
        //   successFun.apply(that, [res.data]);
      } else {
        wx.showToast({
          title: !!res.message.global ? res.message.global : "请求出错",
          icon: "loading"
        })
      }
    })
  },
  // 获取礼包id
  getPkgId(){
    var that=this;
    var orderId = this.data.orderId;
    var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    ajaxhelper.get(app.globalData.frontJSHost + 'present/getPkgIdByOrderId/' + orderId + '?token=' + encodeURIComponent(token), {}, this, function (res) {
      console.log(res);
      if (res.result && res.resultStatus == 200) {
        if (res.data){
          that.setData({
            pkgId:res.data,
            isDisabledButton: false,
          })
        }else{
          that.setData({
            isDisabledButton: true,
          })
        }
      } else {
        that.setData({
          isDisabledButton:true,
        })
        wx.showToast({
          title: !!res.message.global ? res.message.global : "请求出错",
          icon: "loading"
        })
      }
    },function(){
      that.setData({
        isDisabledButton: true,
      })
    })
  },
  // 关闭弹窗
  closeGiftMask(){
    this.setData({
      isShowGiftMask: false,
    })
  }
}))